package edu.ccut.saturn.manager.bundles.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IBundleComponentInfo;
import edu.ccut.saturn.component.IComponentInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnDataManager;
import edu.ccut.saturn.component.impl.SaturnActionComponentInfo;
import edu.ccut.saturn.component.impl.SaturnBaseComponentInfo;
import edu.ccut.saturn.component.impl.SaturnLogicComponentInfo;

public class GetBundleTreeXml extends DefaultComponent {
	
	private static final String IMAGE_PATH = "component/";

	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		//bundle名
		String key = (String) parameterContext.getParameterValue(0);
		//取组件文件结构（component,logic,action）
		IBundleComponentInfo bundleComponentInfo = ComponentManager
				.getInstance().getBundleComponentInfo(key);

		List<String> componentStringList = new ArrayList<String>();
		String type = "";

		if (bundleComponentInfo != null) {
			Collection<IComponentInfo> componentInfos = bundleComponentInfo
					.getComponentInfos();

			Iterator<IComponentInfo> componentIterator = componentInfos
					.iterator();

			while (componentIterator.hasNext()) {
				IComponentInfo componentInfo = componentIterator.next();

				if (componentInfo instanceof SaturnActionComponentInfo) {
					type = "action_saturnfolder";

				} else if (componentInfo instanceof SaturnLogicComponentInfo) {
					type = "logic_saturnfolder";

				} else if (componentInfo instanceof SaturnBaseComponentInfo) {
					type = "component_saturnfolder";

//				} else if (componentInfo instanceof SaturnDataInfo) {
//					type = "data_saturnfolder";
//
//				} else if (componentInfo instanceof SaturnValidateInfo) {
//					type = "validate_saturnfolder";
//
//				} else if (componentInfo instanceof SaturnWorkFlowInfo) {
//					type = "workflow_saturnfolder";
				}
                //形如 /userModule/userLogic/Account.logic
				String componentKey = componentInfo.getKey();
				//形如 /userModule/userLogic
				String componentString = componentKey.substring(0, componentKey
						.lastIndexOf("/"));

				if (componentString.lastIndexOf("/") > 0) {
					type = "module";
					                       //形如 userModule,module
					componentStringList.add(componentString.substring(0,
							componentString.lastIndexOf("/")).replace("/", "")
							+ "," + type);

				} else {
					                       //形如 userLogic,logic_saturnfolder
					componentStringList.add(componentString.replace("/", "")
							+ "," + type);
				}
			}
		}
		//componentStringList内容形如 
		//userModule,module, 
		//userModule,module,  
		//roleModule,module, 
		//userModule,module, 
		//roleModule,module, 
		//userModule,module, 
		//userModule,module
        //取数据项文件结构(saturnData)
		Set<String> saturnDataNamespaceKeys = SaturnDataManager.getInstance()
				.getSaturnDataInfoNamespaceKeys();// data

		if (saturnDataNamespaceKeys != null) {
			for(String saturnDataNameSpaceKey :saturnDataNamespaceKeys){
				//namespace形如 http://saturnManager/dictModule/data
				String[] keyArray = saturnDataNameSpaceKey.substring(7).split(
				"/");
				String bundleName = keyArray[0];
				//只选择当前bundle下的数据项显示
				if (key.equals(bundleName)) {
		
					if (keyArray.length == 2) {
						type = "data_saturnfolder";
		
					} else if (keyArray.length == 3) {
						type = "module";
					}
					componentStringList.add(keyArray[1] + "," + type);
				}
			}
		}

		componentStringList = unique(componentStringList);
		String treeXml = getTreeXml(componentStringList);
		parameterContext.setParameter(1, treeXml);
	}
	
	
	public String getTreeXml(List<String> list) {
		Collections.sort(list);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
				.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><tree id=\"0\">"); //$NON-NLS-1$

		stringBuffer.append("<item text=\"").append("src").append("\" id=\"")
				.append("src").append("\"").append(" im0=\"zzjgs1.gif\"")
				.append(" open=\"1\">");

		for (String nameStr : list) {
			String[] nameAndType = nameStr.split(",");
			stringBuffer.append("<item text=\"").append(nameAndType[0]).append(
					"\" id=\"").append(nameAndType[0]).append("\"").append(
					" im0=\"" + IMAGE_PATH + nameAndType[1] + ".gif\" im1=\""
							+ IMAGE_PATH + nameAndType[1] + ".gif\" im2=\""
							+ IMAGE_PATH + nameAndType[1] + ".gif\"").append(
					" child=\"1\" >").append("</item>");
		}
		stringBuffer.append("</item>");
		stringBuffer.append("</tree>");
		String treeXml = stringBuffer.toString();
		return treeXml;
	}

	/**
	 * 去掉list中的重复项
	 * 
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> unique(List<String> list) {
		Set set = new HashSet();
		set.addAll(list);

		List list1 = new ArrayList();
		list1.addAll(set);

		return list1;
	}
	
}